<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="540px">
    <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
      <el-form-item label="教室名称" :label-width="labelWidth" prop="name">
        <el-input v-model="editForm.name" auto-complete="off" placeholder="请输入教室名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="教室地点" :label-width="labelWidth" prop="localtion">
        <el-input v-model="editForm.localtion" auto-complete="off" placeholder="请输入教室地点" clearable></el-input>
      </el-form-item>
      <el-form-item label="教室类型" :label-width="labelWidth" prop="type">
          <el-select
            v-model="editForm.type"
            clearable
            placeholder="请选择教室类型"
            filterable
          >
            <el-option
              v-for="item in classroomTypeList"
              :key="item.classRoomCode"
              :label="item.classRoomMessage"
              :value="item.classRoomMessage"
            ></el-option>
          </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="hanldeSave('editFormRef')" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
import { addClassRoomInfo, updateClassRoomInfo, getClassRoomType, getClassRoomInfoList } from '@/views/children/basic/js/classRoomInfo'
import pick from 'lodash.pick'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
// import moment from 'moment'
export default {
  name: 'classroomInfoModal',
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      title: '新增教室',
      isAdd: false,
      disabled: false,
      dialogVisible: false,
      labelWidth: '120px',
      editForm: {
        name: '',
        localtion: '',
        type: ''
      },
      model: {},
      rules: {
        name: [{ required: true, validator: this.validateName }],
        localtion: [{ required: true, validator: this.validateLocaltion }],
        type: [{ required: true, validator: this.validateType }]
      },
      classroomTypeList: []
    }
  },
  props: {},
  computed: {},
  filters: {},
  mounted () {
    this.getClassRoomType()
  },
  methods: {
    add () {
      this.edit({})
    },
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      console.log(record)
      this.$nextTick(() => {
        if (this.model.id) {
          this.title = '修改教室'
          this.editForm = pick(this.model, 'name', 'localtion', 'type')
        } else {
          this.title = '新增教室'
          // this.editForm.insideNotice = 0
          // this.editForm.mobileNotice = 0
        }
      })
    },
    // 提交表单
    hanldeSave (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg
          const values = {
            name: this.editForm.name,
            localtion: this.editForm.localtion,
            type: this.editForm.type
          }
          console.log(values)
          if (!this.model.id) {
            res = await addClassRoomInfo(Object.assign({}, values))
            msg = '保存成功！'
          } else {
            res = await updateClassRoomInfo(Object.assign({}, values, { id: this.model.id }))
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },

    handleCancel () {
      this.dialogVisible = false
      this.model = {}
      this.$refs.editFormRef.resetFields()
    },
    async getClassRoomInfoList () {
      let res = await getClassRoomInfoList({ name: this.editForm.name })
      if (res.data.total > 0) {
        return true
      } else {
        return false
      }
    },
    strlen (str) {
      var len = 0
      for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i)
        // 单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
          len++
        } else {
          len += 2
        }
      }
      return len
    },

    validateName (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入教室名称'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('教室名称不能超过30个字符'))
        } else {
          // if (this.getClassRoomInfoList()) {
          //   callback(new Error('教室名称已存在'))
          // }
          callback()
        }
      } else {
        callback(new Error('请输入教室名称'))
      }
    },
    validateLocaltion (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入教室地点'))
        } else if (this.strlen(value) > 30) {
          // console.log(this.strlen(value))
          callback(new Error('教室地点不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入教室地点'))
      }
    },
    validateType (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入教室类型'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('教室类型不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入教室类型'))
      }
    },
    async getClassRoomType () {
      let res = await getClassRoomType()
      console.log(res)
      this.classroomTypeList = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}

/deep/ .el-rate__item {
  padding-top: 5px;
  .el-rate__icon {
    font-size: 22px;
  }
}
/deep/ .img-over {
  margin-right: 5px;
  width: 60px;
  height: 45px;
  cursor: pointer;
}
/deep/ .viewer-container {
  z-index: 3000 !important;
}
.line{
  display: inline-block;
  width: 350px;
}
</style>
